package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.Function;
import top.jnaw.jee.platform.model.FunctionUsers;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Strings;

public class FunctionController extends Controller {

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    renderJson(Strings
        .formatJsonDateTime(Strings.formatPage(Function.list(page, size))));
  }

  public void update() {
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final JSONArray uids = Strings.getJSONArray(req, "uids", null);
    renderJson(FunctionUsers.update(id, uids).toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    if (id > 0) {
      result = Function.query(id);
    }
    renderJson(result.toJson());
  }
}

